package models

import (
	"coding.net/vco/mder/modules/coding"
	"coding.net/vco/mder/modules/util"
	"fmt"
	"github.com/BurntSushi/toml"
)

/*
{
	name:"",
	url:"",
	keywords:""
}
*/

func Catalog_List(root string) []map[string]interface{} {
	dest := coding.DATA_PATH
	if !util.IsBlank(root) { //root 目录
		dest = fmt.Sprintf("%s/%s", dest, root)
	}

	dirs, _ := util.ChildDirs(dest, func(dir string) bool {
		return !(coding.IsIgnorable(dir) || coding.IsStaticDir(dir))
	})
	catalogs := make([]map[string]interface{}, 0)
	for _, dir := range dirs {
		url := fmt.Sprintf("%s/%s", root, dir)
		tomlConfig := fmt.Sprintf("%s/%s/.%s.toml", coding.DATA_PATH, url, dir) //目录同名.toml
		v := map[string]interface{}{}
		toml.DecodeFile(tomlConfig, v)
		item := map[string]interface{}{}
		util.MergeStringKeyMap(item, v)
		item["url"] = url
		if _, exist := item["name"]; !exist {
			item["name"] = dir
		}
		catalogs = append(catalogs, item)
	}

	return catalogs
}
